Freezing a TensorFlow Model


In [1]:
import tensorflow as tf
import numpy as np
from tensorflow.python.tools import freeze_graph

Required files

1) Saved model

2) Saved model's graph


In [2]:
# Freeze the graph
save_path="/Users/Enkay/Documents/Viky/python/lecture/freeze/model_files/" #directory to model files
MODEL_NAME = 'Sample_model' #name of the model optional
input_graph_path = save_path+'savegraph.pbtxt'#complete path to the input graph
checkpoint_path = save_path+'model.ckpt' #complete path to the model's checkpoint file
input_saver_def_path = ""
input_binary = False
output_node_names = "output" #output node's name. Should match to that mentioned in your code
restore_op_name = "save/restore_all"
filename_tensor_name = "save/Const:0"
output_frozen_graph_name = save_path+'frozen_model_'+MODEL_NAME+'.pb' # the name of .pb file you would like to give
clear_devices = True

In [3]:
freeze_graph.freeze_graph(input_graph_path, input_saver_def_path,
                          input_binary, checkpoint_path, output_node_names,
                          restore_op_name, filename_tensor_name,
                          output_frozen_graph_name, clear_devices, "")


INFO:tensorflow:Restoring parameters from /Users/Enkay/Documents/Viky/python/lecture/freeze/model_files/model.ckpt
INFO:tensorflow:Froze 2 variables.
Converted 2 variables to const ops.